package ph.com.gs3.formalistics.view.documentview;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.valueobjects.business.form.FormField.FieldType;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class FieldRequiredNotifier {

	public static final String TAG = FieldRequiredNotifier.class.getSimpleName();

	public static final String PROMPT_FIELD_REQUIRED = "This field is required";

	private View rootView;

	public FieldRequiredNotifier(View rootView) {
		this.rootView = rootView;
	}

	public void notifyField(String fieldId, FieldType fieldType) {

		switch (fieldType) {
			case TEXT_FIELD:
			case TEXT_AREA:
				notifyEditText(fieldId);
				break;
			case CHECK_BOX_GROUP:
				notifyCheckboxGroup(fieldId);
				break;
			case RADIO_BUTTON_GROUP:
				notifyRadioButtonGroup(fieldId);
				break;
			default:
				break;

		}

	}

	private void notifyEditText(String id) {

		EditText etField = (EditText) rootView.findViewWithTag(id);

		if (etField != null) {
			etField.setError(PROMPT_FIELD_REQUIRED);
		} else {
			FLLogger.w(TAG, id + " not found, failed to notify field required.");
		}

	}

	private void notifyCheckboxGroup(String id) {
		LinearLayout llCheckboxesContainer = (LinearLayout) rootView.findViewWithTag(id);

		int checkboxCount = llCheckboxesContainer.getChildCount();

		if (checkboxCount > 0) {
			// set error on the first checkbox found
			final CheckBox cbFirstField = (CheckBox) llCheckboxesContainer.getChildAt(0);
			cbFirstField.setError(PROMPT_FIELD_REQUIRED);

			// add click listeners to all the checkboxes so that the error will be cleared
			// after checking any checkbox
			for (int i = 0; i < checkboxCount; i++) {
				CheckBox cbField = (CheckBox) llCheckboxesContainer.getChildAt(i);
				cbField.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						cbFirstField.setError(null);
						// TODO: add a way to unregister the listeners
					}
				});
			}

		}

	}

	private void notifyRadioButtonGroup(String id) {

		RadioGroup rgField = (RadioGroup) rootView.findViewWithTag(id);

		int radioButtonCount = rgField.getChildCount();

		if (radioButtonCount > 0) {
			// set error on the first radio button found
			final RadioButton rbFirstField = (RadioButton) rgField.getChildAt(0);
			rbFirstField.setError(PROMPT_FIELD_REQUIRED);

			rgField.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					rbFirstField.setError(null);
				}
			});

		}

	}

}
